package com.bootlever.mq.producer.mq;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.bootlever.framework.rocketmq.templates.RocketmqTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class ProducerListener implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(ProducerListener.class);

    @Autowired
    private RocketmqTemplate rocketmqTemplate;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        logger.info("开始启动rocketmq producer");
        try {
            DefaultMQProducer producer = (DefaultMQProducer) rocketmqTemplate.getDefaultMQProducer("test_producer");
            producer.start();
            logger.info("rocketmq producer启动成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
